﻿using Abp.Domain.Entities;
using Abp.Domain.Entities.Auditing;
using FutureApp.Users;
using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace FutureApp.Shop
{
    /// <summary>
    /// 商品分类
    /// </summary>
    [Table("GoodsCategories")]
    public class GoodsCategory : FullAuditedEntity<Guid, User>, IPassivable
    {
        /// <summary>
        /// <see cref="Name"/> 属性的最大长度
        /// </summary>
        public const int MaxNameLength = 30;

        /// <summary>
        /// 名称
        /// </summary>
        [Required]
        [StringLength(MaxNameLength)]
        public virtual string Name { get; set; }

        /// <summary>
        /// 排序号
        /// </summary>
        [Required]
        public virtual int Order { get; set; } = 0;

        /// <summary>
        /// 父品牌Id
        /// </summary>
        public virtual Guid? ParentId { get; set; }

        /// <summary>
        /// 父品牌
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public virtual GoodsCategory Parent { get; set; }

        /// <summary>
        /// 子品牌
        /// </summary>
        [ForeignKey(nameof(ParentId))]
        public virtual ICollection<GoodsCategory> Children { get; set; }

        /// <summary>
        /// 商品模型特征属性组合
        /// </summary>
        [ForeignKey("CategoryId")]
        public virtual ICollection<GoodsCategoryAssemble> CategoryAssembles { get; set; }

        /// <summary>
        /// 商品模型特征属性组合
        /// </summary>
        [NotMapped]
        public ICollection<GoodsProperty> Properties
        {
            get
            {
                return (from assemble in this.CategoryAssembles
                        orderby assemble.Order
                        select assemble.Property
                        ).ToList();
            }
        }

        /// <summary>
        /// 激活/禁用
        /// </summary>
        [Required]
        public virtual bool IsActive { get; set; } = true;
    }
}
